package com.catena.web.websocket;

import com.catena.response.ApiUpdateResponse;
import com.catena.util.JsonUtil;
import org.springframework.stereotype.Component;

import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * Created by hx-pc on 16-11-1.
 */

@ServerEndpoint ("/notice/message")
@Component
public class NoticeMessage extends WebSocketPool {

    @Override
    @OnOpen
    public void onOpen(Session session) {
        super.onOpen(session);
    }

    @Override
    @OnClose
    public void onClose(Session session) {
        super.onClose(session);
    }

    public void sendApiUpdateMessageAll(ApiUpdateResponse apiUpdateResponse) throws IOException, EncodeException {
        for (int i = 0; i < this.getAsyncPool().size(); i++) {
            this.getAsyncPool().get(i).getBasicRemote().sendText(JsonUtil.writeValueAsString(apiUpdateResponse));
        }
    }
}
